package com.alibaba.citrus.ots.platform.enhance.repository.impl;

import com.alibaba.citrus.ots.platform.enhance.repository.InventoryAllotLineRepository;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsplatformenhance.inventoryallotline.dto.CreateInventoryAllotLineResponse;
import com.epoch.app.otsplatformenhance.inventoryallotline.dto.LoadInventoryAllotLineRequest;
import com.epoch.app.otsplatformenhance.inventoryallotline.dto.UpdateInventoryAllotLineResponse;
import com.epoch.app.otsplatformenhance.inventoryallotline.model.InventoryAllotLine;
import com.epoch.app.otsplatformenhance.inventoryallotline.service.InventoryAllotLineService;
import com.epoch.app.otsplatformenhance.model.dto.InventoryAllotLineLoadListRequest;
import com.epoch.app.otsplatformenhance.model.dto.InventoryAllotLineSDO;
import com.epoch.app.otsplatformenhance.model.dto.UpdateAllotLineRequest;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class InventoryAllotLineRepositoryImpl implements InventoryAllotLineRepository {

    @Autowired
    private InventoryAllotLineService inventoryAllotLineService;
    private static final Log log = Log.getLogger(InventoryAllotLineRepositoryImpl.class);
    @Override
    public String create(InventoryAllotLineSDO inventoryAllotLineSDO) {
        InventoryAllotLine inventoryAllotLine = getInventoryAllotLine(inventoryAllotLineSDO);
        CreateInventoryAllotLineResponse inventoryAllotLine1 = inventoryAllotLineService.createInventoryAllotLine(inventoryAllotLine);
        return inventoryAllotLine1.getLastInsertId();
    }

    @Override
    public Long update(InventoryAllotLineSDO inventoryAllotLineSDO) {
        InventoryAllotLine inventoryAllotLine = getInventoryAllotLine(inventoryAllotLineSDO);
        inventoryAllotLine.setId(Long.parseLong(inventoryAllotLineSDO.getId()));
        UpdateInventoryAllotLineResponse resp = inventoryAllotLineService.updateInventoryAllotLine(inventoryAllotLine);
        return 1L;
    }

    @Override
    public Result<List<InventoryAllotLine>> queryAllotList(InventoryAllotLineLoadListRequest request) {
        Result<List<InventoryAllotLine>> listResult = inventoryAllotLineService.loadInventoryAllotLineList(request);
        return listResult;
    }

    @Override
    public InventoryAllotLineSDO queryById(String id) {
        try {
            LoadInventoryAllotLineRequest request = new LoadInventoryAllotLineRequest(Long.parseLong(id));
            InventoryAllotLine inventoryAllotLine = inventoryAllotLineService.loadInventoryAllotLine(request);
            if(inventoryAllotLine.getId() == null){
                log.info("查不到数据库存分解单 id = "+ id);
                return null;
            }
            InventoryAllotLineSDO sdo = new InventoryAllotLineSDO();
            BeanUtils.copyProperties(inventoryAllotLine, sdo);
            sdo.setId(id);
            return sdo;
        } catch (Exception e) {
            log.error("queryById Exception",e);
            return null;
        }
    }

    @Override
    public Integer updateRemainingQuantity(UpdateAllotLineRequest requet) {

        Result<Integer> result = inventoryAllotLineService.updateRemain(requet);
        if (result != null && result.getResult() != null){
            return result.getResult();
        }
        return null;
    }

    @NotNull
    private InventoryAllotLine getInventoryAllotLine(InventoryAllotLineSDO inventoryAllotLineSDO) {
        InventoryAllotLine inventoryAllotLine = new InventoryAllotLine();
        if(StringUtils.isNotBlank(inventoryAllotLineSDO.getId())) {
            inventoryAllotLine.setId(Long.parseLong(inventoryAllotLineSDO.getId()));
        }
        inventoryAllotLine.setInventoryAllotId(inventoryAllotLineSDO.getInventoryAllotId());
        inventoryAllotLine.setScItemId(inventoryAllotLineSDO.getScItemId());
        inventoryAllotLine.setScItemName(inventoryAllotLineSDO.getScItemName());
        inventoryAllotLine.setAvailableInventory(inventoryAllotLineSDO.getAvailableInventory());
        inventoryAllotLine.setAdjustInventory(inventoryAllotLineSDO.getAdjustInventory());
        inventoryAllotLine.setImportInventory(inventoryAllotLineSDO.getImportInventory());
        inventoryAllotLine.setDimensionId(inventoryAllotLineSDO.getDimensionId());
        inventoryAllotLine.setDimensionName(inventoryAllotLineSDO.getDimensionName());
        inventoryAllotLine.setCalculationScale(inventoryAllotLineSDO.getCalculationScale());
        inventoryAllotLine.setDistributionQuantity(inventoryAllotLineSDO.getDistributionQuantity());
        inventoryAllotLine.setInventoryUnit(inventoryAllotLineSDO.getInventoryUnit());
        inventoryAllotLine.setRemainingQuantity(inventoryAllotLineSDO.getRemainingQuantity());
        inventoryAllotLine.setFeature(inventoryAllotLineSDO.getFeatures());
        return inventoryAllotLine;
    }


}
